<?php
/**
 * Created by PhpStorm.
 * User: zhj
 * Date: 2019/3/6
 * Time: 16:36
 */

namespace app\common\logic;

/**
 * 美食逻辑
 */
class Foods extends LogicBase
{

    /**
     * 美食分类编辑
     */
    public function FoodsCategoryEdit($data = [])
    {

        $validate_result = $this->validateFoodsCategory->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateFoodsCategory->getError()];
        }

        $url = url('FoodsCategoryList');

        $result = $this->modelFoodsCategory->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '美食分类' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelFoodsCategory->getError()];
    }

    /**
     * 获取美食列表
     */
    public function getFoodsList($where = [], $field = '*', $order = '',$pagenation='15')
    {

       /* $this->modelFoods->alias('a');

        $join = [
            [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
            [SYS_DB_PREFIX . 'foods_category c', 'a.category_id = c.id'],
        ];

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        $this->modelFoods->join = $join;*/

        return $this->modelFoods->getList($where, $field, $order,$pagenation);
    }

    /**
     * 获取美食列表搜索条件
     */
    public function getWhere($data = [])
    {

        $where = [];

        !empty($data['search_data']) && $where['a.name|a.describe'] = ['like', '%'.$data['search_data'].'%'];

        return $where;
    }

    /**
     * 美食信息编辑
     */
    public function FoodsEdit($data = [])
    {

        $validate_result = $this->validateFoods->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateFoods->getError()];
        }

        $url = url('FoodsList');

        empty($data['id']) && $data['member_id'] = MEMBER_ID;

        $data['content'] = html_entity_decode($data['content']);

        $result = $this->modelFoods->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '美食' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '美食操作成功', $url] : [RESULT_ERROR, $this->modelFoods->getError()];
    }

    /**
     * 获取美食信息
     */
    public function getFoodsInfo($where = [], $field = '*')
    {

       /* $this->modelFoods->alias('a');

        $join = [
            [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
            [SYS_DB_PREFIX . 'Foods_category c', 'a.category_id = c.id'],
        ];

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        $this->modelFoods->join = $join;*/

        return $this->modelFoods->getInfo($where, $field);
    }

    /**
     * 获取分类信息
     */
    public function getFoodsCategoryInfo($where = [], $field = true)
    {

        return $this->modelFoodsCategory->getInfo($where, $field);
    }

    /**
     * 获取美食分类列表
     */
    public function getFoodsCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {

        return $this->modelFoodsCategory->getList($where, $field, $order, $paginate);
    }

    /**
     * 美食分类删除
     */
    public function FoodsCategoryDel($where = [])
    {

        $result = $this->modelFoodsCategory->deleteInfo($where);

        $result && action_log('删除', '美食分类删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '美食分类删除成功'] : [RESULT_ERROR, $this->modelFoodsCategory->getError()];
    }

    /**
     * 美食删除
     */
    public function FoodsDel($where = [])
    {

        $result = $this->modelFoods->deleteInfo($where);

        $result && action_log('删除', '美食删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '美食删除成功'] : [RESULT_ERROR, $this->modelFoods->getError()];
    }
}
